package com.my.wechat.handler;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.my.wechat.builder.NewsBuilder;
import com.my.wechat.builder.TextBuilder;
import com.my.wechat.service.impl.WeChatUserServiceImpl;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

@Component
public class SubscribeHandler extends AbstractHandler {

	@Autowired
	WeChatUserServiceImpl weChatUserServiceImpl;
	
    @Value(value="${sskj.conf.subscribeLinkUrl}")  
    private String subscribeLinkUrl; 

	@Value(value="${sskj.conf.webDomain}")  
    private String webDomain;
	
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService,
			WxSessionManager sessionManager) throws WxErrorException {

		this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());

		// 获取微信用户基本信息
		WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser(), null);

		if (userWxInfo != null) {
			// 添加关注用户到本地
			weChatUserServiceImpl.save(userWxInfo);
		}

		WxMpXmlOutMessage responseResult = null;
		try {
			responseResult = handleSpecial(wxMessage);
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
		}

		if (responseResult != null) {
			return responseResult;
		}

		try {
			String description="欢迎关注宜昌智慧社区公众号，请及时进行实名认证！";
			// return new NewsBuilder().build(description, "", title, "http://"+webDomain+"#"+subscribeLinkUrl, wxMessage);
			return new TextBuilder().build(description, wxMessage, weixinService);
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
		}

		return null;
	}
	
	public WxMpXmlOutNewsMessage handleNews(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService,
			WxSessionManager sessionManager) throws WxErrorException {

		this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());

		// 获取微信用户基本信息
		WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser(), null);

		if (userWxInfo != null) {
			// TODO 可以添加关注用户到本地
			weChatUserServiceImpl.save(userWxInfo);
		}

		WxMpXmlOutNewsMessage responseResult = null;
		try {
			responseResult = handleSpecialNews(wxMessage);
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
		}

		if (responseResult != null) {
			return responseResult;
		}

		try {
			String description="好好好的";
			String picUrl="https://www.baidu.com/img/bd_logo1.png";
			String title="ok,let's go";
			String url="https://www.baidu.com/img/bd_logo1.png";
			return new NewsBuilder().build(description, picUrl, title, url, wxMessage);
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
		}

		return null;
	}

	/**
	 * 处理特殊请求，比如如果是扫码进来的，可以做相应处理
	 */
	private WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage) throws Exception {
		// TODO
		return null;
	}
	
	/**
	 * 处理特殊请求，比如如果是扫码进来的，可以做相应处理
	 */
	private WxMpXmlOutNewsMessage handleSpecialNews(WxMpXmlMessage wxMessage) throws Exception {
		// TODO
		return null;
	}

}
